'use strict';

module.exports = {
  up: async (queryInterface, Sequelize) => {
    const { STRING, INTEGER, DATE } = Sequelize;
    
    try {
      // 添加发票号码字段（如果不存在）
      await queryInterface.addColumn('sales_orders', 'invoice_number', {
        type: STRING(50),
        allowNull: true,
        comment: '发票号码'
      });
    } catch (error) {
      console.log('invoice_number字段可能已存在:', error.message);
    }
    
    try {
      // 添加催款相关字段（如果不存在）
      await queryInterface.addColumn('sales_orders', 'collection_status', {
        type: Sequelize.ENUM('pending', 'contacted', 'promised', 'paid', 'difficult'),
        defaultValue: 'pending',
        allowNull: true,
        comment: '催款状态'
      });
    } catch (error) {
      console.log('collection_status字段可能已存在:', error.message);
    }
    
    try {
      await queryInterface.addColumn('sales_orders', 'collection_count', {
        type: INTEGER,
        defaultValue: 0,
        allowNull: true,
        comment: '催款次数'
      });
    } catch (error) {
      console.log('collection_count字段可能已存在:', error.message);
    }
    
    try {
      await queryInterface.addColumn('sales_orders', 'last_collection_date', {
        type: DATE,
        allowNull: true,
        comment: '最后催款日期'
      });
    } catch (error) {
      console.log('last_collection_date字段可能已存在:', error.message);
    }
    
    try {
      await queryInterface.addColumn('sales_orders', 'last_collection_result', {
        type: Sequelize.STRING(200),
        allowNull: true,
        comment: '最后催款结果'
      });
    } catch (error) {
      console.log('last_collection_result字段可能已存在:', error.message);
    }
    
    try {
      await queryInterface.addColumn('sales_orders', 'next_followup_date', {
        type: DATE,
        allowNull: true,
        comment: '下次跟进日期'
      });
    } catch (error) {
      console.log('next_followup_date字段可能已存在:', error.message);
    }
  },

  down: async (queryInterface, Sequelize) => {
    try {
      // 删除字段
      await queryInterface.removeColumn('sales_orders', 'invoice_number');
    } catch (error) {
      console.log('删除invoice_number字段时出错:', error.message);
    }
    
    try {
      await queryInterface.removeColumn('sales_orders', 'collection_status');
    } catch (error) {
      console.log('删除collection_status字段时出错:', error.message);
    }
    
    try {
      await queryInterface.removeColumn('sales_orders', 'collection_count');
    } catch (error) {
      console.log('删除collection_count字段时出错:', error.message);
    }
    
    try {
      await queryInterface.removeColumn('sales_orders', 'last_collection_date');
    } catch (error) {
      console.log('删除last_collection_date字段时出错:', error.message);
    }
    
    try {
      await queryInterface.removeColumn('sales_orders', 'last_collection_result');
    } catch (error) {
      console.log('删除last_collection_result字段时出错:', error.message);
    }
    
    try {
      await queryInterface.removeColumn('sales_orders', 'next_followup_date');
    } catch (error) {
      console.log('删除next_followup_date字段时出错:', error.message);
    }
  }
};